<?php
/**
 * Created by Vincent ZHANG.
 * Date: 2018-04-17 0017
 * Time: 16:53
 * Description:
 */

function test()
{
    echo 'test';
    echo 'test';
    echo 'test';
    echo 'test';
}


function create_folder($path)
{
    if (!file_exists($path)) { //可以抽像成函数
        mkdir($path);
        $old = umask(0);
        chmod($path, 0755);
        umask($old);//&& chmod($uploads_path,777);
    }
}


//if (!function_exists('__autoload')) {
//    function __autoload($class)
//    {
//        /* 这样写是错误的
//        if ( ! isset($prefix))
//        {
//            static $prefix;
//            $prefix = config_item('subclass_prefix');
//        }; */
//
//        static $prefix;
//
//        ($prefix === NULL) && $prefix = config_item('subclass_prefix');
//
//
//        if (strpos($class, $prefix) === 0) {
//            require(APPPATH . "core/{$class}.php");
//        } else {
//            strpos($class, 'CI_') !== 0 && require(APPPATH . "third_party/{$class}.php");
//        }
//    }
//};

if (!function_exists('format_date')) {
    function format_date($time)
    {
        $t = time() - $time;
        if ($t > 0) {
            $ba = '前';
        } else {
            $ba = '后';
        }

        $f = [
            '31536000' => '年',
            '2592000' => '个月',
            '604800' => '星期',
            '86400' => '天',
            '3600' => '小时',
            '60' => '分钟',
            '1' => '秒'
        ];
        foreach ($f as $k => $v) {
            if (0 != $c = floor(abs($t) / (int)$k)) {
                return $c . $v . $ba;
            }
        }
    }
}

function redis_init($conf){
    $redis = new Redis();
    preg_match('/^([^:]+)(:[0-9]+)?\\/(.+)?/', $conf, $ms);
    $host = $ms[1];
    $port = trim($ms[2], ':');
    $db = $ms[3];
    $redis->connect($host, $port);
    $redis->select($db);
    return $redis;
}